package com.shchuangzhi.smartguard.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.elvishew.xlog.XLog;
import com.shchuangzhi.smartguard.R;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.DBHelper;
import com.shchuangzhi.smartguard.core.InOutRecord;
import com.shchuangzhi.smartguard.core.Mode;
import com.shchuangzhi.smartguard.core.Notify;
import com.shchuangzhi.smartguard.core.NotifyMessage;
import com.shchuangzhi.smartguard.core.NotifyPrinter;
import com.shchuangzhi.smartguard.core.StudentState;
import com.shchuangzhi.smartguard.core.TTS;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainOp extends Fragment {
    private Handler handler1;
    private Handler handler2;
    private TextView tvName;
    private TextView tvStateName;
    private TextView tvNo;
    private TextView tvClassName;
    private TextView tvRecTime;
    private CircleImageView imgView;
    private DBHelper dbHelper;
    public MainOp() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_main_op, container, false);
        tvName = (TextView)view.findViewById(R.id.tv_op_name);
        tvStateName = (TextView)view.findViewById(R.id.tv_op_state);
        tvNo = (TextView)view.findViewById(R.id.tv_op_no);
        tvClassName = (TextView)view.findViewById(R.id.tv_op_class);
        tvRecTime = (TextView)view.findViewById(R.id.tv_op_time);
        imgView = (CircleImageView)view.findViewById(R.id.img_op_head_big);

        dbHelper = new DBHelper(this.getActivity());
        List<InOutRecord> list = dbHelper.getInOutRecords(Mode.getInstance().getCurrent(), 1);
        if (list.size() > 0) {
            setRecCtl(list.get(0));
        }

        handler1 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                try {
                    NotifyMessage message = (NotifyMessage)msg.obj;
                    InOutRecord rec = (InOutRecord)message.getData();
                    setRecCtl(rec);
                    LinearLayout layout = (LinearLayout)view.findViewById(R.id.layout_main_op);
                    layout.setVisibility(View.VISIBLE);
                    if (rec.getState() == Consts.STATE_HEALTH || rec.getState() == Consts.STATE_OUT) {
                        TTS tts = TTS.get();
                        tts.speak(tts.withTpl1(Mode.getInstance().getCurrent(), rec.getName(), rec.getTime()));
                    }
                } catch(Exception e) {
                    XLog.e("", e);
                }
            }
        };

        NotifyPrinter.getInstance().addNotify(Consts.NOTIFY_CHECK_IN_OUT_MAIN, new Notify() {
            @Override
            public void notify(NotifyMessage message) {
                handler1.sendMessage(handler1.obtainMessage(Consts.NOTIFY_CHECK_IN_OUT_MAIN, message));
            }
        });

        handler2 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                LinearLayout layout = (LinearLayout)view.findViewById(R.id.layout_main_op);
                dbHelper = new DBHelper(getActivity());
                List<InOutRecord> list = dbHelper.getInOutRecords(Mode.getInstance().getCurrent(), 1);
                if (list.size() > 0) {
                    setRecCtl(list.get(0));
                    layout.setVisibility(View.VISIBLE);
                } else {
                    layout.setVisibility(View.INVISIBLE);
                }
            }
        };

        NotifyPrinter.getInstance().addNotify(Consts.NOTIFY_MAIN_OP_SWITCH_MODE, new Notify() {
            @Override
            public void notify(NotifyMessage message) {
                handler2.sendMessage(handler2.obtainMessage(Consts.NOTIFY_MAIN_OP_SWITCH_MODE, message));
            }
        });

        return view;
    }

    private void setRecCtl(InOutRecord rec) {
        tvName.setText(rec.getName());
        tvStateName.setText(rec.getStateName());
        tvNo.setText(new Integer(rec.getNo()).toString());
        tvClassName.setText(rec.getClassName());
        tvRecTime.setText(rec.getShortTimeStr());
        imgView.setImageResource(R.drawable.person3);

        switch(rec.getState()) {
            case Consts.STATE_HEALTH: tvStateName.setTextColor(Color.GREEN);
                break;
            case Consts.STATE_WATCH: tvStateName.setTextColor(Color.BLUE);
                break;
            case Consts.STATE_SICK: tvStateName.setTextColor(Color.RED);
                break;
            default:tvStateName.setTextColor(Color.GREEN);
                break;
        }
    }
}
